package com.shanxia.codec.cmd;

import com.shanxia.codec.Decoder;
import com.shanxia.codec.ProtocolDataBase;
import lombok.Data;

/**
 * S -> C
 * 服务器应答心跳包信息
 * 后台服务器对收到充电桩心跳包后的应答
 */
@Data
public class Cmd101 extends ProtocolDataBase {

    private int v1;                           // 预留
    private int v2;                           // 预留
    private int heart;                        // 心跳应答 （累加，到最大值时为 1，重新累加）

    public Cmd101(){
        super.setCmd(101);
    }

    public static Cmd101 decodeFromBase(ProtocolDataBase protocolDataBase) {
        Cmd101 cmd = new Cmd101();
        cmd.copyDataFrom(protocolDataBase);

        int offset = 0;
        cmd.v1 = Decoder.getUint16(protocolDataBase.getDataField(), offset);
        offset += 2;
        cmd.v2 = Decoder.getUint16(protocolDataBase.getDataField(), offset);
        offset += 2;
        // 2字节心跳应答
        cmd.heart = Decoder.getUint16(protocolDataBase.getDataField(), offset);
        return cmd;
    }

    @Override
    public byte[] getDataField() {
        byte[] data = new byte[6];
        Decoder.getBytesAsUint16(data, 0, this.v1);
        Decoder.getBytesAsUint16(data, 2, this.v2);
        Decoder.getBytesAsUint16(data, 4, this.heart);
        return data;
    }
}